package org.rednebula.framework.core.model.domain;

import java.util.List;

import org.rednebula.framework.core.IEnvironment;


public abstract class AbstractQuery {
	protected boolean isWhereAdded;
	protected boolean count;
	protected boolean uniqueResult;
	protected String orderByClause;


	public abstract String hql();

	public Object execute(IEnvironment env) {
		// Query query = session.createQuery(hql());
		// applyParameters(query);
		// applyPage(query);

		return null;
	}

	protected abstract void applyParameters(Object query);

	protected abstract void applyPage(Object query);

	public List<?> untypeList(IEnvironment env) {
		return null;
	}

	private void resetQuery() {
		isWhereAdded = false;
		count = false;
		uniqueResult = false;
	}

	public void appendWhereClause(String whereClause, StringBuilder hql) {
		if (isWhereAdded) {
			hql.append("  and ");
		} else {
			isWhereAdded = true;
			hql.append("where ");
		}
		hql.append(whereClause);
	}

	protected void appendOrderByClause(StringBuilder hql) {
		if (orderByClause != null) {
			hql.append("order by ");
			hql.append(orderByClause);
		}
	}

	public void addOrderByClause(String clause) {
		if (orderByClause == null) {
			orderByClause = clause;
		} else {
			orderByClause += ", " + clause;
		}
	}
}
